//
//  ZDTopView.m
//  tableView test new
//
//  Created by Apple on 15/10/18.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDTopView.h"
#import "UIView+ZUtility.h"

#define ZDHeadImageViewWH 70
#define ZDNavigationBarHeight 64

#define ZDContentOffsetKey @"contentOffset"

@interface ZDTopView ()

@property (nonatomic, strong) UIImageView *backgroundImageView;

@property (nonatomic, strong) UIImageView *headImageView;

@property (nonatomic, strong) UILabel *title;

@property (nonatomic, strong) UILabel *subTitle;


@end

@implementation ZDTopView

- (instancetype)initWithFrame:(CGRect)frame andBackgroundImageName:(NSString *)backgroundImageName andHeadImageName:(NSString *)headImageName andTitle:(NSString *)title andSubTitle:(NSString *)subTitle
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
        
        _backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView.frame = CGRectMake(0, -frame.size.height * 0.5, frame.size.width, frame.size.height * 1.5);
        _backgroundImageView.image = [UIImage imageNamed:backgroundImageName];
        
        
        _headImageView = [[UIImageView alloc] init];
        _headImageView.frame = CGRectMake((frame.size.width - ZDHeadImageViewWH) * 0.5, (frame.size.height - ZDHeadImageViewWH) * 0.5, ZDHeadImageViewWH, ZDHeadImageViewWH);
        _headImageView.image = [UIImage imageNamed:headImageName];
        
        _headImageView.layer.cornerRadius = ZDHeadImageViewWH * 0.5;
        _headImageView.layer.masksToBounds = YES;
        
        
        _title = [[UILabel alloc] init];
        _title.frame = CGRectMake(0, 0.65*frame.size.height, frame.size.width, 0.2*frame.size.height);
        _title.text = title;
        _title.textAlignment = NSTextAlignmentCenter;
        _title.textColor = [UIColor whiteColor];
        _title.font = [UIFont systemFontOfSize:18.0];
        
        
        _subTitle = [[UILabel alloc] init];
        _subTitle.frame = CGRectMake(0, 0.8*frame.size.height, frame.size.width, 0.1*frame.size.height);
        _subTitle.text = subTitle;
        _subTitle.textAlignment = NSTextAlignmentCenter;
        _subTitle.textColor = [UIColor whiteColor];
        _subTitle.font = [UIFont systemFontOfSize:15.0];
        
        
        [self addSubview:_backgroundImageView];
        [self addSubview:_headImageView];
        [self addSubview:_title];
        [self addSubview:_subTitle];
        
        self.clipsToBounds = YES;
        
        
        //为self添加点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        
        [self addGestureRecognizer:tapGesture];
    }
    
    return self;
}


#pragma mark - 点击手势回调方法
- (void)tapAction:(UITapGestureRecognizer *)recognizer
{
    if (self.headImageClickBlock)
    {
        self.headImageClickBlock();
    }
}


#pragma mark - 将要添加到父控件时调用
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    //注册kvo监听scrollView contentoffset的改变
    [self.scrollView addObserver:self forKeyPath:ZDContentOffsetKey options:NSKeyValueObservingOptionNew context:nil];
    
    self.scrollView.contentInset = UIEdgeInsetsMake(self.height, 0, 0, 0);
}


#pragma mark - KVO回调方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
//    NSLog(@"%@", change);
    
    if ([keyPath isEqualToString:ZDContentOffsetKey])
    {
        CGPoint contentOffset = [change[@"new"] CGPointValue];
        
        [self updateSubviewsWithContentOffset:contentOffset];
    }
}


#pragma mark - 私有方法 根据偏移更新子控件
- (void)updateSubviewsWithContentOffset:(CGPoint)newOffset
{
    //目标偏移
    CGFloat destOffset = -ZDNavigationBarHeight;
    
    //开始偏移
    CGFloat startOffset = -self.scrollView.contentInset.top;
    
    //筛选偏移  小于开始偏移 或者大于目标偏移的都剔除
//    newOffset = CGPointMake(newOffset.x, newOffset.y<startOffset?startOffset:(newOffset.y>destOffset?destOffset:newOffset.y));
    
    if (newOffset.y < startOffset)
    {
        newOffset.y = startOffset;
    }
    else if (newOffset.y > destOffset)
    {
        newOffset.y = destOffset;
    }
    
    NSLog(@"%@", NSStringFromCGPoint(newOffset));
    
    
    //设置子视图的偏移量
    CGFloat subviewOffset = self.height-40;
    
    //修改当前view的y坐标，偏移量取反
    CGFloat newY = -newOffset.y-self.scrollView.contentInset.top;
    
    //最大偏移量
    CGFloat distance = destOffset-startOffset;
    
    //计算透明度
    CGFloat alpha = 1-(newOffset.y-startOffset) / distance;
    
    //计算图片的缩小系数
    CGFloat imageReduce = 1-(newOffset.y-startOffset) / (distance*2);

    
    self.subTitle.alpha = alpha;
    self.title.alpha = alpha;
    
    //修改当前view的y坐标，偏移量取反
    self.frame = CGRectMake(0, newY, self.width, self.height);
    
    self.backgroundImageView.frame = CGRectMake(0, -0.5*self.height+(1.5*self.height-ZDNavigationBarHeight)*(1-alpha), self.backgroundImageView.width, self.backgroundImageView.height);
    
    //平移
    CGAffineTransform t = CGAffineTransformMakeTranslation(0,(subviewOffset-0.42*self.height)*(1-alpha));
    
    //缩放
    self.headImageView.transform = CGAffineTransformScale(t, imageReduce, imageReduce);
    
    self.title.frame = CGRectMake(0, 0.65*self.height+(subviewOffset-0.45*self.height)*(1-alpha), self.width, 0.2*self.height);
    self.subTitle.frame = CGRectMake(0, 0.8*self.height+(subviewOffset-0.45*self.height)*(1-alpha), self.width, 0.1*self.height);
}



#pragma mark - 注销KVO
- (void)dealloc
{
    [self.scrollView removeObserver:self forKeyPath:ZDContentOffsetKey];
}

@end
